package com.zekdot.registerclient.util;

import org.springframework.util.DigestUtils;

import java.util.Date;
import java.util.Random;

/**
 * Create by zekdot on 20-3-21.
 */
public class IdUtil {
    private static Random random = new Random();
    /**
     * 获取MD5
     * @param value
     * @return
     */
    public static String getMd5(String value) {
        value=new StringBuffer().append(value).toString(); //加盐做md5
        return DigestUtils.md5DigestAsHex(value.getBytes());
    }

    /**
     * 获取报名id
     * @param username
     * @param title
     * @param time
     * @return
     */
    public static String getId(String username,String title,String time){
        return getMd5(random.nextInt() + username + title + time);
    }


    /**
     * 根据报名id生成token 计算规则:MD5(随机数+eId+时间+用户密码) 可以在SQL语句中直接完成计算
     * @param eId 报名id
     * @param password 用户密码
     * @return token
     */
    public static String getEnrollToken(String eId,String password){
        StringBuilder sb = new StringBuilder();
        sb.append(random.nextInt());
        sb.append(eId);
        sb.append(password);
        sb.append(new Date().getTime());
        return getMd5(sb.toString());
    }
}
